/**create by liuhua at 2019年6月3日 上午10:14:28**/
package com.star.truffle.core.web.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import com.star.truffle.core.StarServiceException;
import com.star.truffle.core.security.dto.StarUserInfo;
import com.star.truffle.core.util.MemoryCacheUtil;
import com.star.truffle.core.web.ApiCode;

public class BaseService {

  public StarUserInfo getLoginInfo(String token) {
    Authentication auth = SecurityContextHolder.getContext().getAuthentication();
    StarUserInfo starUserInfo = null;
    if (null == auth) {
      if (StringUtils.isNotBlank(token)) {
        starUserInfo = MemoryCacheUtil.get(token).orElse(null);
      }
    } else {
      starUserInfo = (StarUserInfo) auth.getPrincipal();
    }
    if (null == starUserInfo) {
      throw new StarServiceException(ApiCode.SECURITY_TOKEN_NOT_ACTIVE);
    }
    return starUserInfo;
  }
}
